package cn.hl.sfzy.controller;

import cn.hl.sfzy.entity.Course;
import cn.hl.sfzy.entity.CourseType;
import cn.hl.sfzy.entity.UserCourse;
import cn.hl.sfzy.service.CourseTypeService;
import cn.hl.sfzy.service.UserCourseService;
import cn.hl.sfzy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserCourseController
 * @Description TODO
 * @Author libowen
 * @Date 2024年10月30日 16:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/UserCourse")
@CrossOrigin
public class UserCourseController {

    @Autowired
    private UserCourseService userCourseService;

    @GetMapping("/findCourseByUserId")
    public Result selectCourseByUserId(Long user_id) {
        List<Course> courses = userCourseService.getCourseByUserId(user_id);
        return Result.success(courses);
    }

    @PostMapping("/add")
    public Result insert(UserCourse userCourse) {
        int i = userCourseService.addUserCourse(userCourse);
        return Result.success();
    }

    @GetMapping("/findIdByUserAndCourse")
    public Result selectIdByUserAndCourse(UserCourse userCourse) {
        int id = userCourseService.getIdByUserAndCourse(userCourse);
        return Result.success(id);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        int num = userCourseService.delete(id);
        if(num!=0){
            return Result.success("删除成功！");
        }else{
            return Result.error("删除失败！");
        }
    }

    @PostMapping("/deleteByUserAndCourse")
    public Result deleteByUserAndCourse(UserCourse userCourse) {
        int num = userCourseService.deleteByUserAndCourse(userCourse);
        if(num!=0){
            return Result.success("删除成功！");
        }else{
            return Result.error("删除失败！");
        }
    }
    @GetMapping("/updatejia")
    public Result updatejia( Integer id){
        System.out.println(id);
        userCourseService.updatejia(id);
        return Result.success("积分增加");
    }
    @GetMapping ("/updatejian")
    public Result updatejian( Integer id){
        System.out.println(id);
        userCourseService.updatejian(id);
        return Result.success("积分减少");
    }

}
